<!-- Personal_Information_window.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人信息添加</title>
    <link rel="stylesheet" href="css/personal.css">
</head>
<body>
<span class="close-btn" onclick="closeWindow()">&times;</span>
<div class="container">
    <form action="#" method="post">
        <h1 align="center" style="color: #FFFFFF">个人信息</h1>
        <div class="form-group">
            <label for="username">用户名：</label>
            <input type="text" name="username" id="username" required>
        </div>
        <br><br>

        <div class="form-group">
            <label for="userphone">电话号码：</label>
            <input type="text" name="userphone" id="userphone" required>
        </div>
        <br><br>

        <div class="form-group">
            <label for="useremail">邮箱：</label>
            <input type="text" name="useremail" id="useremail" required>
        </div>
        <br><br>

        <div class="form-group">
            <label >性别：</label>
            <input type="radio" name="gender" value="1" id="male" required>
            <label for="male">男
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <input type="radio" name="gender" value="2" id="female" required>
            <label for="female">女
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
        </div>
        <br><br>



        <br><br>

        <div class="button-container">
            <button type="submit" class="button" onclick="submitPerson()">
                <div class="button__label">提交</div>
                <div class="button__icon"></div>
            </button>
            <button type="button" class="button" id="off">
                <div class="button__label">注销</div>
                <div class="button__icon"></div>
            </button>
            <button type="button" class="button">
                <div class="button__label" onclick="submitLoginOut()">退出登录</div>
                <div class="button__icon"></div>
            </button>
        </div>


    </form>
</div>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="js/window.js"></script>
<script>
    //获取按钮元素
    var button = document.getElementById('off');
    //监听按钮点击事件
    button.addEventListener('click', function() {
        var isConfirmed = confirm("确定要注销吗？");
        if (isConfirmed) {
           submitOff();
        }
        else{
            return;
        }
    });
    function submitPerson(){
        const username=document.getElementById('username').value;
        const telephone=document.getElementById('userphone').value;
        const email=document.getElementById('useremail').value;
        const userGender = document.querySelector('input[name="gender"]:checked').value;

        // 构造请求数据
        const requestData = `username=${encodeURIComponent(username)}&telephone=${encodeURIComponent(telephone)}&email=${encodeURIComponent(email)}&userGender=${encodeURIComponent(userGender)}`;
      axios({
          method: "post",
          url: "http://localhost:8080/javaweb_project/user/changeUser",
          headers: {
              "Content-Type": "application/x-www-form-urlencoded"
          },
          data: requestData

      }).then(function (response) {
            // 处理响应
            console.log(response.data);
            if (response.data === 'changeSucceed') {
                alert("修改成功");
                closeWindow();
                window.location.href = 'http://localhost:8080/javaweb_project/index.html'
                window.parent.postMessage({ action: 'loginSuccess', username: username }, '*');// 发送消息给父窗口
                localStorage.setItem('username', username); // 设置用户名到localStorage,用来存储用户登录状态
                window.parent.updateUserInfoForParent(username); // 直接调用父页面的更新函数
            } else if (response.data === 'changeUserFail') {
                alert("修改失败")
            }
        }).catch(function (error) {
            // 处理错误
            console.error('Error during login:', error);
            document.getElementById("username_err").style.display = '';
            alert('修改过程中出现了错误，请再试一次!');
        });

    }
    function submitLoginOut(){
        axios({
            method: "post",
            url: "http://localhost:8080/javaweb_project/user/loginOut",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        }).then(function (response){
            // 处理响应
            console.log(response.data);
            if(response.data==='loginOutSucceed'){
                closeWindow();
                window.location.href = 'http://localhost:8080/javaweb_project/index.html'
                localStorage.removeItem('username');
                window.parent.checkLoginStatus();
            }else if(response.data==='loginOutFailed'){
                alert("退出失败")
            }
        }).catch(function (error) {
            // 处理错误
            console.error('Error during login:', error);
            document.getElementById("username_err").style.display = '';
            alert('退出过程中出现了错误，请再试一次!');
        });
    }

    function submitOff(){

        axios({
            method:"post",
            url:"http://localhost:8080/javaweb_project/user/deleteUser",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        }).then(function (response) {
            console.log(response.data);
                    if (response.data === 'deleteUserSucceed') {
                        closeWindow();
                        // 重定向到登录页面
                        window.location.href = 'http://localhost:8080/javaweb_project/index.html';
                        localStorage.removeItem('username');
                        window.parent.checkLoginStatus();
                    } else {
                        // 显示错误消息
                        alert(response.data.message);
                    }
                }).catch(function (error) {
                // 处理错误
                console.error('注销失败:', error);
                alert('注销过程中出现了错误，请再试一次!');
            });


    }



</script>
</body>
</html>